<?php


function list_per_komponen($pr_yearly_income_id, $komponen_id){
	my_set_code_js('
		function resetConfirm(){
			var t = confirm(\'Data akan direset?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=upload_reset&pr_yearly_income_id='.$_GET['pr_yearly_income_id'].'&komponen_id='.$_GET['komponen_id'].'\';
			}
			return false;
		}
			
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=upload_delete&pr_yearly_income_id='.$_GET['pr_yearly_income_id'].'&komponen_id='.$_GET['komponen_id'].'&karyawan_id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'NIK'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:65%'),  
		'Nominal'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		//' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	if(isset($_GET['key']))
		$query = "SELECT a.karyawan_id , a.nominal FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE a.pr_yearly_income_id={$pr_yearly_income_id} 
			AND a.komponen_id={$komponen_id}  AND a.nominal <> 0 
			AND ( c.nik='{$_GET['key']}' OR d.nama LIKE '%{$_GET['key']}%' )
			ORDER BY c.NIK
			";
	else
		$query = " SELECT a.karyawan_id , a.nominal FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.pr_yearly_income_id=".$pr_yearly_income_id."
			AND a.komponen_id=".$komponen_id ." AND a.nominal <> 0"
		;  
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =7;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		  
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&pr_yearly_income_id={$_GET['pr_yearly_income_id']}&komponen_id={$_GET['komponen_id']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){ 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);

		$editproperty = array(
			'href'=>'index.php?com=pr_yearly_income&task=edit_upload
				&pr_yearly_income_id='.$pr_yearly_income_id.'
				&komponen_id='.$komponen_id.'
				&karyawan_id='.$ey['karyawan_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 
		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['karyawan_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'nik'=>position_text_align ($karyawan['nik'] ,'center'),    
			'nama'=> $karyawan['nama_gelar'] ,      
			'nominal'=>position_text_align('Rp. '.rp_format($ey['nominal']) , 'right')  ,
			//'operasi'=> position_text_align( $edit_button .' '.$delete_button, 'right'),
		); 
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();	

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .='<table width="100%"   style="border-collapse:collapse;border-color:white" cellspacing="0" cellpadding="2">';
	$view .= form_field_display( '<span class="label_form"><b>'.get_komponen($pr_yearly_income_id,$komponen_id).'</b></span>', "<b>Komponen</b>"    ); 
	$view .= '<tr><td><br/>'.$search .'</td><td valign="bottom"><div style="width:100%;text-align:right;">
	 <input type="button" value="Excel" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&task=excel&komponen_id='.$komponen_id.'&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/>   
	 <input type="button" value="Kembali" class="main_button" onclick="javascript:location.href=\'index.php?com=pr_yearly_income&pr_yearly_income_id='.$pr_yearly_income_id.'\'"/></div></td></tr>'   ; 
	return  $view.table_builder($header , $datas ,  6 , false ,$paging );	

}


function get_komponen($pr_yearly_income_id,$komponen_id){

	$query = "
			SELECT label  		
			FROM pr_yearly_income_komponen
			WHERE pr_yearly_income_id={$pr_yearly_income_id} 
			AND komponen_id={$komponen_id} 
			LIMIT 1
		";
	$result = my_query($query );
	$results=my_fetch_array($result) ;
	return $results['label'] ;
}	

function excel_report_komponen($pr_yearly_income_id , $komponen_id ){
	my_component_load('xl_builder' , false);
	$query = " SELECT a.karyawan_id , a.nominal FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.pr_yearly_income_id=".$pr_yearly_income_id."
			AND a.komponen_id=".$komponen_id 
		;
	$res  = my_query($query);
	$header =  array();
	$header['NIK']=array();
	$header['Nama']=array();
	$header['Nominal']=array();
	 
	$row = array(); 
	while($ey = my_fetch_array($res )){ 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$row[] = array(
			'NIK'=>"'".$karyawan['nik'],
			'Nama'=>$karyawan['nama'],
			'Nominal'=>$ey['nominal'],
		);
	}

	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,  24 ,false );
}	